package com.htgraph.util;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxUtils;
import com.mxgraph.io.mxCodec;
import com.ht.impl.GraphEditor;
import org.w3c.dom.Document;

import java.io.IOException;
import java.util.Map;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Wang jiaxiang
 *         Date: 2010-7-15
 */
public class CellFactory {
    public static mxCell createVolCell() {
        Document document = null;
        try {
            document = mxUtils.parseXml(mxUtils
                    .readFile(GraphEditor.class.getResource("/mxefile/vol.mxe").getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mxCodec codec = new mxCodec(document);
        codec.decode(document.getDocumentElement());
        Map<String, Object> cs = codec.getObjects();
        mxCell cc = null;
        mxCell rr = null;
        for (Object o : cs.values()) {
            if (o instanceof mxCell)
                cc = (mxCell) o;
            if (cc.getGeometry() != null) {
                rr = cc;
            }
        }
        return rr;
    }

    public static mxCell createRecCell() {
        Document document = null;
        try {
            document = mxUtils.parseXml(mxUtils
                    .readFile(GraphEditor.class.getResource("/mxefile/rec.mxe").getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mxCodec codec = new mxCodec(document);
        codec.decode(document.getDocumentElement());
        Map<String, Object> cs = codec.getObjects();
        mxCell cc = null;
        mxCell rr = null;
        for (Object o : cs.values()) {
            if (o instanceof mxCell)
                cc = (mxCell) o;
            if (cc.getGeometry() != null) {
                rr = cc;
            }
        }
        return rr;
    }

    public static void fillCells(Map<mxCell, mxCell[]> cell2Paint) {
        //todo:
//        for (mxCell cell : cell2Paint.keySet()) {
//            mxCell[] cells = cell2Paint.get(cell);
//            mxCell volCell = createVolCell();
//            mxCell volCell1 = createVolCell();
//            mxCell recCell = createRecCell();
//            Rectangle rec = cell.getGeometry().getRectangle();
//            double x = rec.getX();
//            double y = rec.getY();
//            double h = rec.getHeight();
//            volCell.getGeometry().setX(x + 5);
//            volCell.getGeometry().setY(y + h);
//            volCell1.getGeometry().setX(x + 20);
//            volCell1.getGeometry().setY(y + h);
//            recCell.getGeometry().setX(x + 50);
//            recCell.getGeometry().setY(y + h);
//
//            cells[0] = volCell;
//            cells[1] = volCell1;
//            cells[2] = recCell;
//        }
    }
}
